package utils.export;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import utils.primitif.StringUtils;


public class CSVUtils {
	private static String path = "C:\\Users\\Gary Benattar\\workspace\\GTrend\\";
	public static boolean generateCsvFile(String sFileName,String sFolder, String sExtenstion, 
			ArrayList<Double> ArrayListData,ArrayList<Double> ArrayListZscore, ArrayList<Date> ArraylistDate)
	{
		if(sFileName == null || sFileName.equals("") || sFileName.equals(" ")){
			System.err.println("Filename null or empty");
			return false;
		}
		if(ArrayListData == null || ArraylistDate == null || ArrayListZscore == null){
			System.err.println("ArrayList null");
			return false;
		}
		if(ArrayListData.size() != ArraylistDate.size() - 1 && ArrayListZscore.size() != ArrayListData.size()){
			System.err.println("Error while checking Data arrayList and Date Array list");
			return false;
		}
		try
		{
			FileWriter writer = new FileWriter(path + sFolder + "//" + sFileName + sExtenstion);

			for(int i = 0; i < ArrayListData.size(); i++){
				writer.append(StringUtils.getStringFromDate(ArraylistDate.get(i + 1)));
				writer.append(',');
				writer.append(ArrayListData.get(i).toString());
				writer.append(',');
				writer.append(ArrayListZscore.get(i).toString());
				writer.append('\n');
			}
			writer.flush();
			writer.close();
			return true;
		}
		catch(IOException e)
		{
			e.printStackTrace();
			System.err.println("Error during csv generation");
			return false;
		} 
	}
	public static String CsvToJs(String sFileName){
		if(sFileName == null || sFileName.equals("") || sFileName.equals(" ")){
			System.err.println("Filename null or empty");
			return "";
		}
		StringBuffer buff = new StringBuffer();
		String nbRow = "";
		try {
			
			FileReader reader = new FileReader(path + "Csv//"+ sFileName + ".csv");
			BufferedReader br = new BufferedReader(reader);
			String s;
	
			int cpt = 0;
			while((s = br.readLine()) != null) {
				String[] currentLine = s.split(",");
				if(currentLine.length > 1){
					String[] currentDate = currentLine[0].split("-");
						if(currentDate.length > 2){
							buff.append(String.format("data.setValue(%s, 0, new Date(%s, %s ,%s));", cpt,
									currentDate[0],currentDate[1],currentDate[2]));
							buff.append(String.format("data.setValue(%s, 1, %s);", cpt,currentLine[1]));
							buff.append(String.format("data.setValue(%s, 2, %s);", cpt,currentLine[2]));
						}
				}
				cpt++;
			}
			nbRow = String.format("data.addColumn('date', 'Date'); data.addColumn('number', 'Word'); data.addColumn('number', 'Word');data.addColumn('string', 'title1');data.addColumn('string', 'text1');data.addRows(%s); ",cpt);
		     
		     
		      
			reader.close(); 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("Error during csv to Js");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nbRow + buff.toString();
	}
}
